<template>
  <div style="padding: 10px">
    <div>
      <el-button type="primary"  stripe  @click="add">新增</el-button>
<!--      <el-button type="primary" >导入</el-button>-->
<!--      <el-button type="primary" >导出</el-button>-->
    </div>
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%" clearable></el-input>
      <el-button type="primary" style="margin: 10px" @click="load">查询</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column fixed prop="id" sortable label="ID" width="150"/>
      <el-table-column prop="userName" label="用户名" width="120"/>
<!--      <el-table-column prop="nickname" label="昵称" width="120"/>-->
<!--      <el-table-column prop="sex" label="性别" width="120"/>-->
<!--      <el-table-column prop="age" label="年龄" width="120"/>-->
<!--      <el-table-column label="性别">-->
<!--        <template #default="scope">-->
<!--          <span v-if="scope.row.gender==='0'">男</span>-->
<!--          <span v-if="scope.row.gender==='1'">女</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="身份" width="120">
        <template #default="scope">
          <span v-if="scope.row.status==='0'">总经理</span>
          <span v-if="scope.row.status==='3'">会计部门</span>
          <span v-if="scope.row.status==='1'">销售部门</span>
          <span v-if="scope.row.status==='2'">采购部门</span>
          <span v-if="scope.row.status==='4'">普通用户</span>
        </template>
      </el-table-column>>
      <el-table-column prop="email" label="邮箱" width="120"/>
      <el-table-column prop="address" label="地址" width="120"/>
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确认删除?" @confirm="handleDelete(scope.row)">
            <template #reference>
              <el-button type="text">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin:10px 0">
      <el-pagination
          :page-size="20"
          :pager-count="11"
          layout="prev, pager, next"
          :total="100"
      >
      </el-pagination>

      <el-dialog v-model="dialogVisible" title="Tips" width="30%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="用户名">
            <el-input v-model="form.userName" style="width: 80%"></el-input>
          </el-form-item>

          <el-form-item label="邮箱">
            <el-input v-model="form.email" style="width: 80%" disabled></el-input>
          </el-form-item>
<!--          <el-form-item label="昵称">-->
<!--            <el-input v-model="form.nickname" style="width: 80%"></el-input>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="年龄">-->
<!--            <el-input v-model="form.age" style="width: 80%"></el-input>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="性别">-->
<!--            <el-radio v-model="form.gender" label="0">男</el-radio>-->
<!--            <el-radio v-model="form.gender" label="1">女</el-radio>-->
<!--            <el-radio v-model="form.gender" label="2">未知</el-radio>-->
<!--          </el-form-item>-->
          <el-form-item label="身份">
            <el-radio v-model="form.status" label="0">总经理</el-radio>
            <el-radio v-model="form.status" label="1">普通管理员</el-radio>
            <el-radio v-model="form.status" label="2">用户</el-radio>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="form.address" style="width: 80%"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
        </template>
      </el-dialog>
    </div>
  </div>

</template>

<script>
import request from "@/utils/request";
export default {
  name: 'Home',
  components: {},
  data() {
    return {
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      total: 10,
      tableData: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request.get("/user", {
        // params: {
        //   pageNum: this.currentPage,
        //   pageSize: this.pageSize,
        //   search: this.search
        // }
      }).then(res => {
        console.log(res)
        this.tableData = res.data
        console.log(this.tableData)
      })
    },
    add() {
      this.dialogVisible = true
      this.form = {}
    },
    save() {
      console.log(this.form)
      let u={
        "address":this.form.address,
        "gender":this.form.gender,
        "id":this.form.id,
        "status":this.form.status,
        "userName":this.form.userName,
        "email":this.form.email,
        "userPwd":this.form.userPwd,
        'bankName':this.form.bankName,
        "createTime":this.form.createTime,

      }


      console.log(u)
      if (this.form.id!=null) {
        //更新
        request.put("/user", u).then(res => {
          console.log(res);
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          }
          this.load();
          this.dialogVisible=false;
        })
      } else {
        request.post("/user/register", u).then(res => {
          console.log(res);
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          }
          this.load();
          this.dialogVisible=false;
        })
      }
    },
    handleDelete(row){
      console.log(row.id);
      request.delete("/user/"+row.id).then(res=>{
        if(res.code === '0'){
          this.$message({
            type:"success",
            message:"删除成功"
          })
        }else {
          this.$message({
            type:"error",
            message:"删除失败"
          })
        }
      });
      this.load();
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    }
  }

}

</script>
